﻿
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQml 2.15
import Qt.labs.platform 1.1
import FluentUI 1.0
import App 1.0

FluScrollablePage{
    function tranParityStringToInt(parity_str){
        if(parity_str === qsTr("无")) {
            return 0
        }
        if(parity_str === qsTr("奇校验")) {
            return 3
        }
        if(parity_str === qsTr("偶校验")) {
            return 2
        }
    }
    launchMode:FluPageType.SingleInstance
    FluFrame{
        Layout.fillWidth: true
        Layout.preferredHeight: 75
        Layout.topMargin: 20
        padding: 10
        ColumnLayout{
            spacing:5
            FluText{
                text: qsTr("连接")
                font: FluTextStyle.BodyStrong
                Layout.bottomMargin: 4
            }
            FluRadioButtons{
                spacing: 8
                orientation: Qt.Horizontal
                FluRadioButton{
                    text:qsTr("串口通信")
                }
                Component.onCompleted: {
                    currentIndex = 0
                }
            }
        }
    }
    FluFrame {
        Layout.fillWidth: true
        Layout.preferredHeight: 180
        Layout.topMargin: 20
        padding: 10
        ColumnLayout{
            spacing: 10
            FluText{
                id:title
                text: qsTr("串口配置")
                font: FluTextStyle.BodyStrong
                Layout.bottomMargin: 4
            }
            RowLayout {
                spacing: 10
                RowLayout{
                    FluText{
                      text:qsTr("串口号")
                    }
                    FluComboBox{
                        id:comm_port_combo
                        editable: false
                        model:ListModel {
                            id:serial_ports_model
                            Component.onCompleted: {
                               for(var i in LinkManager.serialPorts){
                                    append({text:LinkManager.serialPorts[i]})
                               }
                               comm_port_combo.currentIndex = 0
                            }
                        }
                    }
                }
                RowLayout {
                    FluText{
                      text:qsTr("波特率")
                    }
                    FluComboBox{
                        id:baud_combo
                        editable: false
                        model:[9600,19200,38400,57600,115200]
                    }
                }
                RowLayout{
                    FluText{
                     text:qsTr("校验位")
                   }
                   FluComboBox{
                       id:parity_combo
                       editable: false
                       model:[qsTr("无"),qsTr("奇校验"),qsTr("偶校验")]
                   }
                }
            }
            RowLayout {
                spacing: 10
                RowLayout{
                    FluText{
                      text:qsTr("数据位")
                    }
                    FluComboBox{
                        id:data_bits_combo
                        editable: false
                        model:[8,7,6]
                    }
                }
                RowLayout {
                    FluText{
                      text:qsTr("停止位")
                    }
                    FluComboBox{
                        id:stop_bits_combo
                        editable: false
                        model:[1,1.5,2]
                    }
                }
            }
            RowLayout{
                Layout.preferredWidth: parent.width
                FluFilledButton{
                    Layout.alignment: Qt.AlignCenter
                    text:qsTr("刷新串口")
                    Layout.preferredWidth: 120
                    onClicked: {
                        LinkManager.updateSerialPorts();
                        serial_ports_model.clear()
                        for(var i in LinkManager.serialPorts){
                             serial_ports_model.append({text:LinkManager.serialPorts[i]})
                        }
                        comm_port_combo.currentIndex = 0
                        showSuccess(qsTr("串口刷新成功"))
                    }
                }
                FluFilledButton{
                    id:connect_btn
                    Layout.alignment: Qt.AlignCenter
                    text: enabled? qsTr("连接"):qsTr("已连接")
                    enabled: !ControllerManager.towerDevice.connected
                    Layout.preferredWidth: 120
                    onClicked: {
                            var port_name = comm_port_combo.currentText
                            var baud =  baud_combo.currentText
                            var data_bits = data_bits_combo.currentText
                            var stop_bits  = stop_bits_combo.currentText
                            var parity  = tranParityStringToInt(parity_combo.currentText)
                            var connected = ControllerManager.towerDevice.connectLink(
                                port_name,
                                baud  ,
                                data_bits,
                                stop_bits,
                                parity
                            )
                            connected  === true ?
                                showSuccess(qsTr("连接成功")) :showError(qsTr("连接失败"))
                        }
                    }
                    FluFilledButton{
                        Layout.alignment: Qt.AlignCenter
                        text:qsTr("断开连接")
                        Layout.preferredWidth: 120
                        enabled: !connect_btn.enabled
                        onClicked: {
                            ControllerManager.towerDevice.disConnectedLink()
                        }
                    }
                    FluFilledButton{
                        enabled: connect_btn.enabled
                        Layout.alignment: Qt.AlignCenter
                        text:qsTr("启动调试连接")
                        Layout.preferredWidth: 120
                        onClicked: {
                            //打开调试界面
                            //SetDebugger//
                            var port_name = comm_port_combo.currentText
                            var baud =  baud_combo.currentText
                            var data_bits = data_bits_combo.currentText
                            var stop_bits  = stop_bits_combo.currentText
                            var parity  = tranParityStringToInt(parity_combo.currentText)
                            var connected = ControllerManager.towerDevice.connectDebuggerLink(
                                port_name,
                                baud  ,
                                data_bits,
                                stop_bits,
                                parity )
                            if(connected){
                                showSuccess(qsTr("Debugger调试打开成功"))
                                FluRouter.navigate("/SerialDebugger")
                            }
                            else{
                                showError(qsTr("Debugger调试连接失败"))
                            }
                        }
                    }
                }
            }
        }
}
